package it.sevenbits.app.parser;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CmdParser {
    
    private final Pattern pattern = Pattern.compile("(\"[^\"]*\")|([^\\s]*)");
    private ArrayList<String> allMatches = null;
    private Matcher matcher;
    private int size;
    
    public CmdParser() {
        allMatches = new ArrayList<String>();
        matcher = null;
    };
    
    public String[] parse(String cmd) {
        matcher = pattern.matcher(cmd);
        allMatches.clear();
        String match;
        while (matcher.find()) {
            match = matcher.group();
            if (match.compareTo("") != 0) {
                allMatches.add(match);
            }
        }

        size = allMatches.size();
        if (size > 0) {
            return allMatches.toArray(new String[size]);
        } else {
            return new String[0];
        }
    }
}
